
package {

    import flash.display.Sprite;
    import flash.display.LineScaleMode;
    import flash.display.CapsStyle;
    import flash.display.JointStyle;
    import flash.display.DisplayObject;

    import flash.events.Event;
    import flash.events.MouseEvent;

    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldAutoSize;

    public class ImageButton extends Sprite {

	private var __borderColor : int = 0xcccccc;
	private var __fillColor : int = 0xcc0000;
	private var __width : int = 80;
	private var __height : int = 60;
	private var __borderWidth : int = 5;
	private var __picture : DisplayObject;
	private var __textOnTop : Boolean = false;

	private var __textHeight : int = 72;
	
	private var labelField : TextField;
	private var container : Sprite;
	private var format : TextFormat = new TextFormat();

	public function ImageButton(label:String,picture:DisplayObject,
			       callback:Function) {
	    buttonMode = true;

	    labelField = new TextField();
	    labelField.text = label;
            labelField.selectable = false;
            labelField.mouseEnabled = false;

	    format.size = 24;
	    format.color = 0xcccccc;

	    labelField.setTextFormat(format); 
	    labelField.autoSize = TextFieldAutoSize.CENTER;

	    container = new Sprite();
	    container.addChild(labelField);
	    
	    __picture = picture;
	    container.addChild(picture);

	    addChild(container);
	    centerContainer();

	    if (callback != null) {
		addEventListener(MouseEvent.MOUSE_DOWN,callback);
	    }

	    redraw();
	}

	public function redraw() : void {
	    drawBorder();

	    var trueWidth : int = __width - 2*__borderWidth;
	    var trueHeight : int = __height - 2*__borderWidth;
	    
	    __picture.scaleX = 1;
	    __picture.scaleY = 1;

	    if (__picture.width > trueWidth) {
		__picture.width = trueWidth;
	    }

	    if (__picture.height > trueHeight * 8 / 10) {
		__picture.height = trueHeight * 8 / 10;
	    }

	    if (__picture.scaleX != __picture.scaleY) {
		var minScale :Number = Math.min(__picture.scaleX,
						__picture.scaleY);
		__picture.scaleX = minScale;
		__picture.scaleY = minScale;
	    }

	    var textSpace : int = trueHeight - __picture.height;
	    var adjustedHeight : int = __textHeight;
	    format.size = adjustedHeight;
	    labelField.setTextFormat(format);

	    while (labelField.textHeight > textSpace) {
		adjustedHeight--;
		format.size = adjustedHeight;
		labelField.setTextFormat(format);
	    }

	    while (labelField.textWidth > trueWidth) {
		adjustedHeight--;
		format.size = adjustedHeight;
		labelField.setTextFormat(format);
	    }

	    labelField.x = -labelField.textWidth/2;
	    __picture.x = -__picture.width/2;
	    if (__textOnTop) {
		labelField.y = __borderWidth;
		__picture.y = __borderWidth + labelField.textHeight;
	    } else {
		__picture.y = __borderWidth;
		labelField.y = __borderWidth + __picture.height;
	    }

	    centerContainer();
	}

	public function drawBorder() : void {
	    graphics.clear();
	    graphics.beginFill(__fillColor,100);
	    graphics.lineStyle(__borderWidth,__borderColor,1.0,
			       true,LineScaleMode.NORMAL,
			       CapsStyle.ROUND,JointStyle.ROUND);
	    var halfBorder : int = borderWidth/2;
	    graphics.drawRect(__borderWidth/2,__borderWidth/2,
			      __width-__borderWidth,__height-__borderWidth);
	}

	public function centerContainer() : void {
	    container.x = __width/2;
	    container.y = (__height-labelField.textHeight-__picture.height)/2 -
		borderWidth;
	}

	public function set borderColor(newColor:int) : void {
	    __borderColor = newColor;
	    redraw();
	}

	public function get borderColor() : int {
	    return __borderColor;
	}

	public function set fillColor(newColor:int) : void {
	    __fillColor = newColor;
	    redraw();
	}

	public function get fillColor() : int {
	    return __fillColor;
	}

	public function set textColor(newColor:Object) : void {
	    format.color = newColor;
	    labelField.setTextFormat(format);
	}

	public function get textColor() : Object {
	    return format.color;
	}

	public function set buttonWidth(newWidth:int) : void {
	    if (newWidth > 0) {
		__width = newWidth;
		//		centerContainer();
		redraw();
	    }
	}

	public function get buttonWidth() : int {
	    return __width;
	}

	public function set buttonHeight(newHeight:int) : void {
	    if (newHeight > 0) {
		__height = newHeight;
		//		centerContainer();
		redraw();
	    }
	}
	
	public function get buttonHeight() : int {
	    return __height;
	}

	public function set textSize(newSize:int) : void {
	    if (newSize > 0) {
		format.size = newSize;
		labelField.setTextFormat(format); 
		redraw();
	    }
	}

	public function get textSize() : int {
	    return format.size as int;
	}

	public function set borderWidth(newWidth:int):void {
	    if (newWidth > 0) {
		__borderWidth = newWidth;
		redraw();
	    }
	}

	public function get borderWidth():int {
	    return __borderWidth;
	}

	public function set picture(pict:DisplayObject):void {
	    __picture = pict;
	    redraw();
	}

	public function get picture():DisplayObject {
	    return __picture;
	}

	public function set textOnTop(flag:Boolean):void {
	    __textOnTop = flag;
	    redraw();
	}

	public function get textOnTop():Boolean {
	    return __textOnTop;
	}
    }
}
